/* ZkUtils.java 
 * Created 28 Sep 2011
*/
package com.fastwire.alarm.common.view.util;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.util.resource.Labels;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zul.Image;
import org.zkoss.zul.Messagebox;

import com.fastwire.alarm.admin.model.bussinessobject.AlMsStaff;
import com.fastwire.alarm.admin.model.bussinessobject.AlMsUser;
import com.fastwire.alarm.common.view.constant.SessionAttributes;

/**
 * @author sigit <br>
 * @version 1.0 <br>
 * Application : alarmmanagement <br>
 * Copyright (c) fastwire-group 2011 <br>
 * <br>
 */

public class ZkUtils {
	
	private static Log log = LogFactory.getLog(ZkUtils.class);

	public static void showErrorMessage(String title, String message) {
		try {
			Messagebox.show(message, title != null ? title : Labels.getLabel("MsgBox_Error_Title"), Messagebox.OK, Messagebox.ERROR);
		} catch (InterruptedException e) {
			log.error("Error while displaying ERROR Messagebox.", e);
		}
	}
	
	public static void showInfoMessage(String title, String message) {
		try {
			Messagebox.show(message, title != null ? title : Labels.getLabel("MsgBox_Information_Title"), Messagebox.OK, Messagebox.INFORMATION);
		} catch (InterruptedException e) {
			log.error("Error while displaying INFORMATION Messagebox.", e);
		}
	}
	
	public static void showWarningMessage(String title, String message) {
		try {
			Messagebox.show(message, title != null ? title : Labels.getLabel("MsgBox_Warning_Title"), Messagebox.OK, Messagebox.EXCLAMATION);
		} catch (InterruptedException e) {
			log.error("Error while displaying EXCLAMATION Messagebox.", e);;
		}
	}
	
	public static Integer showQuestionMessage(String title, String message) {
		try {
			return Messagebox.show(message, title != null ? title : Labels.getLabel("MsgBox_Question_Title"), Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION);
		} catch (InterruptedException e) {
			log.error("Error while displaying QUESTION Messagebox.", e);
			return null;
		}
	}
	
	public static void showErrorMessage(String message) {
		showErrorMessage(null, message);
	}
	
	public static void showInfoMessage(String message) {
		showInfoMessage(null, message);
	}

	public static void showWarningMessage(String message) {
		showWarningMessage(null, message);
	}

	public static Integer showQuestionMessage(String message) {
		return showQuestionMessage(null, message);
	}

	public static AlMsUser getLoggedUser() {
		AlMsUser alMsUser = new AlMsUser();
		
		return alMsUser;
	}
	
	
	/**
	 * Used to redirect to a page.
	 */
	public static void sendRedirect(String page) {
		if (null != page && page.length() > 0)
			Executions.sendRedirect(page);
	}

    public static void doLogin() {
		
	}

	public static void doLogout() {
		Sessions.getCurrent().removeAttribute(SessionAttributes.LOGGED_USER);
		Executions.sendRedirect(ZkResourcePath.LOGIN_PAGE);
	}
	
	public static void setLocaleID() {
		Sessions.getCurrent().setAttribute(Attributes.PREFERRED_LOCALE, new Locale("id", "id"));
		Executions.sendRedirect("");
	}
	
	public static void setLocaleEN() {
		Sessions.getCurrent().setAttribute(Attributes.PREFERRED_LOCALE, new Locale("en", "en"));
		Executions.sendRedirect("");
	}
	
	public static String getLocale() {
		Object locale = Sessions.getCurrent().getAttribute(Attributes.PREFERRED_LOCALE);
		if (locale != null && locale instanceof Locale) {
			return ((Locale) locale).getLanguage();
		}
		return "";
	}
	
	public static boolean isLocaleItalian() {
		return "in".equals(getLocale()) || "id".equals(getLocale());
	}
	
	public static boolean isLocaleEnglish() {
		return "en".equals(getLocale());
	}
	
	public static void setDesktopWidth(int width) {
		Sessions.getCurrent().setAttribute(SessionAttributes.DESKTOP_WIDTH, width);
	}
	
	public static void setDesktopHeight(int height) {
		Sessions.getCurrent().setAttribute(SessionAttributes.DESKTOP_HEIGHT, height);
	}
	
	public static int getDesktopWidth() {
		Object width = Sessions.getCurrent().getAttribute(SessionAttributes.DESKTOP_WIDTH);
		if (width != null && width instanceof Integer)
			return (Integer) width;
		return 0;
	}
	
	public static int getDesktopHeight() {
		Object height = Sessions.getCurrent().getAttribute(SessionAttributes.DESKTOP_HEIGHT);
		if (height != null && height instanceof Integer)
			return (Integer) height;
		return 700;
	}
	
	public static int getMainPanelContentWidth() {
		int width = getDesktopWidth();
		if (width != 0)
			return width - 78;
		return 0;
	}
	
	public static int getMainPanelContentHeight() {
		int height = getDesktopHeight();
		if (height != 0)
			return height - 130;
		return 0;
	}
}
